<?php

/**
 * Description of Generic
 *
 * @author Kam
 */
class Application_Form_Catalog_Generic extends My_Form_Frontened {

    protected $type;
    protected $bean;

    /** @var Zend_From_Element[] */
    protected $attributes;
    protected $template;

    public function __construct($type, $bean, $options = null) {
        $this->type = $type;
        $this->bean = $bean;
        parent::__construct($options);
    }

    public function init() {
        $this->setTitle($this->translate('XXX_CATALOG_ADD_FORM_TITLE'));

        $basicGroup = array();
        $attrGroup = array();

        $daoCatalogCategory = new Application_Model_DAO_CatalogCategory();
        $cats = $daoCatalogCategory->getFullCategories();
        $options = array('' => '');
        foreach ($cats as $mainCat) {
            if (!empty($mainCat['categories'])) {
                $tmp = array();
                foreach ($mainCat['categories'] as $cat) {
                    $tmp[$cat['catalog_category_id']] = $cat['name'];
                }
                $options[$mainCat['name']] = $tmp;
            }
        }
        $category = new Zend_Form_Element_Select('catalog_category_id');
        $category->setLabel(_('XXX_CATALOG_ADD_FORM_CATEGORY_LABEL'));
        $category->setRequired();
        $category->addMultiOptions($options);
        $this->addElement($category);
        $basicGroup[] = $category;

        $title = new Zend_Form_Element_Text('title');
        $title->setLabel(_('XXX_CATALOG_ADD_FORM_TITLE_LABEL'));
        $title->setRequired();
        $title->setAttrib('class', 'element-big');
        $this->addElement($title);
        $basicGroup[] = $title;

        $isPaid = $this->type == Application_Model_DAO_Catalog::TYPE_PAID;

        if ($isPaid) {
            $description = new My_Form_Element_Wyswig('description');
            $description->addFilter(new Zend_Filter_StripTags(array(
                        'allowTags' => array('b', 'br', 'i', 'u', 'ul', 'li', 'ol', 'blockquote'),
                    )));
            $description->setLabel(_('XXX_CATALOG_ADD_FORM_DESCRIPTION_LABEL'));
            $description->setAttrib('class', 'element-big');
            $this->addElement($description);
            $basicGroup[] = $description;

            $photo = new My_Form_Element_ImageUpload('image_id');
            $photo->setSourceName('catalog');
//            $photo = new Zend_Form_Element_File('image_id');
            $photo->setLabel(_('XXX_CATALOG_ADD_FORM_IMAGE_LABEL'));
            $photo->setAttrib('class', 'element-big');
            $this->addElement($photo);
            $basicGroup[] = $photo;
        }


        $address = new Zend_Form_Element_Text('address');
        $address->setLabel(_('XXX_CATALOG_ADD_FORM_ADDRESS_LABEL'));
        $this->addElement($address);
        $attrGroup[] = $address;

        $city = new Zend_Form_Element_Text('city');
        $city->setLabel(_('XXX_CATALOG_ADD_FORM_CITY_LABEL'));
        $this->addElement($city);
        $attrGroup[] = $city;

        $zipCode = new Zend_Form_Element_Text('zip_code');
        $zipCode->setLabel(_('XXX_CATALOG_ADD_FORM_ZIP_CODE_LABEL'));
        $this->addElement($zipCode);
        $attrGroup[] = $zipCode;

        $phone = new Zend_Form_Element_Text('phone');
        $phone->setLabel(_('XXX_CATALOG_ADD_FORM_PHONE_LABEL'));
        $this->addElement($phone);
        $attrGroup[] = $phone;


        if ($isPaid) {
            $email = new Zend_Form_Element_Text('email');
            $email->setLabel(_('XXX_CATALOG_ADD_FORM_EMAIL_LABEL'));
            $this->addElement($email);
            $attrGroup[] = $email;
        }

        if ($isPaid) {
            $web = new Zend_Form_Element_Text('web');
            $web->setLabel(_('XXX_CATALOG_ADD_FORM_WEB_LABEL'));
            $this->addElement($web);
            $attrGroup[] = $web;
        }


        $this->_addFormCancel = true;
        $this->_formCancelOptions['position'] = 'left';
        $this->_formCancelOptions['label'] = $this->translate('XXX_CATALOG_ADD_FORM_CANCEL_LABEL');
        $this->_formCancelOptions['url'] = $this->getView()->url(array('action' => 'step-prev'), 'default', false);
        $this->addSubmitElement()->setLabel(_('XXX_CATALOG_ADD_FORM_SUBMIT_BTN'));

        $this->addDisplayGroup($basicGroup, 'basic', array('legend' => _('XXX_CATALOG_ADD_FORM_SECTION_BASIC')));

//        d(array_values($this->attributes));
        $this->addDisplayGroup($attrGroup, 'attr', array('legend' => _('XXX_CATALOG_ADD_FORM_SECTION_ADDITIONAL')));

        $this->addDisplayGroup(array($this->getSubmitElement()), 'action', array('legend' => ''));

        $dec = array(
            'FormElements',
            array('Fieldset', array('class' => 'span9')),
//            array('HtmlTag', array('tag' => 'div', '____closeOnly' => true))
        );
        $this->getDisplayGroup('basic')->setDecorators($dec);
        $this->getDisplayGroup('attr')->setDecorators($dec);
        $this->getDisplayGroup('action')->setDecorators($dec);
    }

    public function ___loadDefaultDecorators() {
        $elems = $this->getElements();
        /* @var $e Zend_Form_Element */
        foreach ($elems as $e) {
//            if ($e instanceof Zend_Form_Element_File) {
//                continue;
//            }
            $spanRow = 'span2x2';
            $isControlGroupCSS = true;
            $options = array('tag' => 'td');
            $elementType = $this->getElementType($e);
            $class = 'type-' . $elementType . ' element-' . $e->getId();
            if ($e->hasErrors()) {
                $class .= ' error';
            }

            $decorators = array();
            if ($e instanceof My_Form_Element_ImageUpload) {
                $decorators[] = 'ImageUpload';
            } elseif ($e instanceof Zend_Form_Element_File) {
                $decorators[] = 'File';
            } else {
                $decorators[] = 'ViewHelper';
            }
            $elementCss = $e->getAttrib('class');
            $e->setAttrib('class', '');

            if ($e instanceof Zend_Form_Element_Button || $e instanceof Zend_Form_Element_Submit) {
                $e->setAttrib('class', 'form_button button ' . $e->getAttrib('class'));
            }
            if ($e instanceof Zend_Form_Element_Submit) {
                $e->setAttrib('class', 'btn btn-primary btn-large submit ' . $e->getAttrib('class'));
                if ($this->_addFormCancel) {
                    $decorators[] = array('FormCancel', $this->_formCancelOptions);
                }
                $isControlGroupCSS = false;
                $spanRow = 'span123123';
            }
            $decorators[] = array('Description', array('tag' => 'div', 'class' => 'description'));
            $decorators[] = 'Errors';
            $options['class'] = $class;
            if ($e instanceof My_Form_Element_Description) {
                $decorators[] = array(array('data' => 'HtmlTag'), array('tag' => 'div', 'colspan' => 2, 'class' => ($isControlGroupCSS ? 'controls ' : '') . 'data type-' . $elementType . ' element-' . $e->getId()));
            } else {
                $decorators[] = array(array('data' => 'HtmlTag'), array('tag' => 'div', 'class' => ($isControlGroupCSS ? 'controls ' : '') . 'data type-' . $elementType . ' element-' . $e->getId()));
                if ($this->_showLabels) {
                    if ($e instanceof Zend_Form_Element_Button || $e instanceof Zend_Form_Element_Submit) {
                        $decorators[] = array('Label', array('tag' => 'div', 'class' => 'label-hidden', 'tagClass' => 'label-column ' . ($isControlGroupCSS ? 'control-label' : '')));
                    } else {
                        $decorators[] = array('Label', array('tag' => 'div', 'class' => 'label', 'tagClass' => 'label-column ' . ($isControlGroupCSS ? 'control-label' : '')));
                    }
                }
            }
            $decorators[] = array(array('row' => 'HtmlTag'), array('tag' => 'div', 'class' => ($isControlGroupCSS ? 'control-group ' : '' ) . ' row ' . $spanRow . ' row-type-' . $elementType . ' row-' . $e->getId() . ' ' . str_replace(array('btn-primary', 'btn-large', 'btn-normal', 'btn-mini', 'btn-small', 'btn'), '', $elementCss)));
            $e->setDecorators($decorators);
        }
        $class = $this->getAttrib('class');
        if (!$class) {
            $class = '';
        }
        $this->setAttrib('class', 'form ' . $this->_mainCss . ' ' . $class);
        $formTitleCss = empty($this->_mainCss) ? '' : $this->_mainCss . '-title';
        $this->setDecorators(array(
            array('FormErrors', array(
                    'ignoreSubForms' => false,
                    'showCustomFormErrors' => true,
                    'onlyCustomFormErrors' => true,
                    'markupElementLabelEnd' => '</b>',
                    'markupElementLabelStart' => '<b>',
                    'markupListItemEnd' => '',
                    'markupListItemStart' => '',
                    'markupListStart' => '<div class="form-errors">',
                    'markupListEnd' => '</div>',
                )
            ),
            'FormElements',
            array('HtmlTag', array('tag' => 'div', 'class' => 'form_elements row')),
            'form',
            array('FormTitle', array('class' => $formTitleCss)),
        ));
    }

    public function render(Zend_View_Interface $view = null) {
        $elements = $this->getElements();
        /* @var $e Zend_Form_Element */
        foreach ($elements as $e) {
            $decorator = $e->getDecorator('row');
            $class = $decorator->getOption('class');
            if ($e->hasErrors()) {
                $class .= ' error';
            } else {
                $class .= ' info';
            }
            $decorator->setOption('class', $class);
        }
        return parent::render($view);
    }

}
